<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain @click="handleAdd">新增</el-button>

        <el-button plain @click="translation">打印</el-button>
      </el-col>
      <right-toolbar @queryTable="Refresh"></right-toolbar>
    </el-row>

    <vxe-table v-loading="tableData.loading" :data="tableData.list" border stripe>
      <vxe-column type="checkbox" width="50" align="center" />
      <vxe-column width="100" title="状态" field="state" align="center">
        <template #default="{ row }">
          <dict-tag :options="apply_state" :value="row.state" />
        </template>
      </vxe-column>

      <vxe-column title="编号" field="number">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small" @click="handleDetail(row)">
            {{ row.number }}
          </el-button>
        </template>
      </vxe-column>
      <vxe-column title="数量" field="num"></vxe-column>
      <vxe-column title="申请人" field="createUserName"></vxe-column>
      <vxe-column title="领用人" field="targetUserName"></vxe-column>
      <vxe-column title="备注" field="remark"></vxe-column>

      <vxe-column title="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small"
            v-if="row.state === 0 || row.state === 3" @click="handleSub(row)">
            提交
          </el-button>

          <el-button style="margin-right: 10px;" link type="primary" size="small"
            v-if="row.state === 0 || row.state === 3" @click="handleUpdate(row)">
            修改
          </el-button>

          <el-button v-if="row.state === 0 || row.state === 3" size="small" link type="primary"
            @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <pagination v-show="tableData.paging.total > 0" :total="tableData.paging.total"
      v-model:page="tableData.paging.pageNum" v-model:limit="tableData.paging.pageSize" @pagination="getList" />

    <el-dialog v-model="isPrintdDialogVisible" width="50%">
      <printd-dialog ref="printdDialog" @sub="getList"></printd-dialog>
    </el-dialog>

    <edit :apply-type="applyType" ref="edit" @sub="handleAddSuccess"></edit>
    <detail ref="detailDialog"></detail>
  </div>
</template>

<script setup>
import api from "./api.js";
import Edit from './edit.vue'
import  DispDialog from './disposal.vue'
const edit = ref();
const dispDialog = ref();
const isDispDialogVisible = ref(false);
const { proxy } = getCurrentInstance();
const { apply_state } = proxy.useDict("apply_state");

// 申请类型
const applyType = ref('ZCCL');

const queryParams = reactive({
  applyType: applyType.value,
});

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

function getList() {
  tableData.loading = true;
  const {pageNum, pageSize} = tableData.paging;

  const query = {...queryParams};
  const pageReq = { page: pageNum - 1, size: pageSize };
  api.list(query, pageReq).then(response => {
    const { content, totalElements } = response
    tableData.list = content;
    tableData.total = totalElements;
    tableData.loading = false;

  });
}



/** 新增操作成功后的回调 */
function handleAddSuccess() {
  // 新增成功后的处理
  proxy.$message.success("新增成功");
  getList();  // 刷新数据
}
function Refresh(){
  proxy.$message.success("刷新成功");
}
function handleAdd() {
  edit.value.open();
}

function handleUpdate(row) {
  edit.value.open(row);
}

function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除').then(function() {
    return api.del(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleSub(row) {
  api.sub(row.id).then(()=> {
    this.getList();
  });
}
function translation() {
  isDispDialogVisible.value = true;
    }

    function handleDialogClose() {
      isDispDialogVisible.value = false;
    }
onMounted(()=> {
  getList()
})

</script>
  